﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using System.Collections.Generic;
using KenLib.BusinessManager;
using KenLib.BusinessObject;

public partial class JOINT_frmJ_TraineeAcademicInput : System.Web.UI.Page
{
    応募学歴情報Manager _応募学歴情報Manager = new 応募学歴情報Manager();
    応募情報Manager _応募情報Manager = new 応募情報Manager();
    //Chua remove Convert.ToInt32( Session["ID_frmJ_TraineeResumeAInput"])
   
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["frmJ_TraineeAcademicInput_Refresh"] = Session["frmJ_TraineeAcademicInput_Refresh"];
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {

        ((JOINT_MstPage_JOINT)this.Master).changeTitle("来社後　学歴情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeAcademicInput");
        RFV_ddl_学歴区分.ErrorMessage = GetError("ER00007");
        RFV_入学年月日月.ErrorMessage = GetError("ER00007");
        RFV_入学年月日年.ErrorMessage = GetError("ER00007");
        RFV_卒業年月日月.ErrorMessage = GetError("ER00007");
        RFV_卒業年月日年.ErrorMessage = GetError("ER00007");
        RFV_学校名.ErrorMessage = GetError("ER00001");
        CV_卒業年月日年.ErrorMessage = GetError("ER00005");
        CV_卒業年月日月.ErrorMessage = GetError("ER00005");
        ddl_学歴区分.Focus();
        if (!IsPostBack)
        {
            Session["frmJ_TraineeAcademicInput_Refresh"] = Server.UrlEncode(System.DateTime.Now.ToString());
            ViewState["page"] = 0;
            if (Session["ID_frmJ_TraineeResumeAInput"] == null)
            {
                Response.Redirect("frmJ_TraineeResumeAInput.aspx");
            }
            DataTable dt = frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
            Load_ddl_入学年月日月();
            Load_ddl_入学年月日年(Convert.ToDateTime(dt.Rows[0]["生年月日"]).Year);
            Load_ddl_卒業年月日月();
            Load_ddl_卒業年月日年(Convert.ToDateTime(dt.Rows[0]["生年月日"]).Year);
            ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
            
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go page frmJ_TraineeInterviewList.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnキャンセル_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeInterviewList.aspx");
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go page frmJ_TraineeWorkedCompInput.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn職歴登録へ_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeWorkedCompInput.aspx");
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go page frmJ_TraineeResumeTranslate
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn翻訳_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeResumeTranslate.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_入学年月日月
    /// </summary>
    public void Load_ddl_入学年月日月()
    {
        List<string> list = new List<string>();
        for (int i = 1; i < 13; i++)
        {
            list.Add(i.ToString());
        }
        ddl_入学年月日月.DataSource = list;
        ddl_入学年月日月.DataBind();
        ListItem item = new ListItem("[月]", "-1");
        item.Selected = true;
        ddl_入学年月日月.Items.Insert(0,item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_入学年月日年
    /// </summary>
    public void Load_ddl_入学年月日年(int int_YearInput)
    {
        List<string> list = new List<string>();
        for (int i = int_YearInput; i <= DateTime.Now.Year; i++)
        {
            list.Add(i.ToString());
        }
        ddl_入学年月日年.DataSource = list;
        ddl_入学年月日年.DataBind();
        ListItem item = new ListItem("[年]", "-1");
        item.Selected = true;
        ddl_入学年月日年.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_入学年月日年
    /// </summary>
    public void Load_ddl_卒業年月日月()
    {
        List<string> list = new List<string>();
        for (int i = 1; i < 13; i++)
        {
            list.Add(i.ToString());
        }
        ddl_卒業年月日月.DataSource = list;
        ddl_卒業年月日月.DataBind();
        ListItem item = new ListItem("[月]", "-1");
        item.Selected = true;
        ddl_卒業年月日月.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in ddl_卒業年月日年
    /// </summary>
    public void Load_ddl_卒業年月日年(int int_YearInput)
    {
        List<string> list = new List<string>();
        for (int i = int_YearInput; i <= DateTime.Now.Year; i++)
        {
            list.Add(i.ToString());
        }
        ddl_卒業年月日年.DataSource = list;
        ddl_卒業年月日年.DataBind();
        ListItem item = new ListItem("[年]", "-1");
        item.Selected = true;
        ddl_卒業年月日年.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data gridview empty
    /// </summary>
    /// <param name="grv"></param>
    /// <param name="strTableHeader"></param>
    /// <param name="strEmpty"></param>
    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count + 1;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in gridview
    /// </summary>
    /// <param name="id"></param>
    public void ShowGrv(int id)
    {
        DataTable dt = new DataTable();
        dt = _応募学歴情報Manager.frmJ_TraineeAcademicInput_SelectByID(id);

        if (dt.Rows.Count == 0)
        {
            //AppCode_Common.ShowEmtyGrid<応募学歴情報>(grv, 4, "データがありません。", "");
            //return;
            string strHeader = "学歴区分,学校名,開始時期,終了時期";
            ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
        }
        else
        {
            grv.DataSource = dt;
            grv.DataBind();
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Double in gridview with RowDataBound
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("ondblclick", "javascript:__doPostBack('ctl00$ContentPlaceHolder1$grv','DoubleClick$0"+e.Row.RowIndex+"')");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Double in gridview with RowCommand
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DoubleClick" && e.CommandArgument.ToString() != "0-1")
        {
            int numberpager = Convert.ToInt32(ViewState["page"]);
            int intRows = 0;
            if (numberpager == 0)
            {
                intRows = Convert.ToInt32(e.CommandArgument);
            }
            else
            {
                intRows = Convert.ToInt32(e.CommandArgument) + (numberpager * 10);
            }
            //string abc = grv.Rows[int.Parse(e.CommandArgument.ToString())].Cells[3].Text.ToString();
            DataTable dt = new DataTable();
            dt = _応募学歴情報Manager.frmJ_TraineeAcademicInput_SelectByID(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
            if (dt.Rows.Count > 0)
            {
                //学歴区分     
                if (dt.Rows[intRows]["学歴区分"].ToString() != "")
                {
                    ListItem item = ddl_学歴区分.Items.FindByValue(dt.Rows[intRows]["学歴区分"].ToString());
                    ddl_学歴区分.SelectedIndex = ddl_学歴区分.Items.IndexOf(item);
                }

                //開始時期
                if (dt.Rows[intRows]["開始時期"].ToString() != "")
                {
                    string str_開始時期 = dt.Rows[intRows]["開始時期"].ToString();
                    string str_開始時期_year = str_開始時期.Substring(0, 4);
                    string str_開始時期_month = str_開始時期.Substring(5, 2);
                    if (Convert.ToInt32(str_開始時期_month) < 10)
                        str_開始時期_month = str_開始時期_month.Substring(1, 1);
                    ListItem item = ddl_入学年月日月.Items.FindByText(str_開始時期_month);
                    ddl_入学年月日月.SelectedIndex = ddl_入学年月日月.Items.IndexOf(item);
                    ListItem item1 = ddl_入学年月日年.Items.FindByText(str_開始時期_year);
                    ddl_入学年月日年.SelectedIndex = ddl_入学年月日年.Items.IndexOf(item1);
                }

                //終了時期
                if (dt.Rows[intRows]["終了時期"].ToString() != "")
                {
                    string str_終了時期 = dt.Rows[intRows]["終了時期"].ToString();
                    string str_終了時期_year = str_終了時期.Substring(0, 4);
                    string str_終了時期_month = str_終了時期.Substring(5, 2);
                    if (Convert.ToInt32(str_終了時期_month) < 10)
                        str_終了時期_month = str_終了時期_month.Substring(1, 1);
                    ListItem item = ddl_卒業年月日月.Items.FindByText(str_終了時期_month);
                    ddl_卒業年月日月.SelectedIndex = ddl_卒業年月日月.Items.IndexOf(item);
                    ListItem item1 = ddl_卒業年月日年.Items.FindByText(str_終了時期_year);
                    ddl_卒業年月日年.SelectedIndex = ddl_卒業年月日年.Items.IndexOf(item1);
                }

                //学校名
                txt_学校名.Text = ConvertTexttoHTML(dt.Rows[intRows]["学校名"].ToString());

                //学校住所
                txt_学校住所.Text = ConvertTexttoHTML(dt.Rows[intRows]["学校住所"].ToString());

                //専攻区分
                if (dt.Rows[intRows]["専攻区分"].ToString() == "True")
                {
                    ListItem lst = ddl_専攻区分.Items.FindByText("文系");
                    ddl_専攻区分.SelectedIndex = ddl_専攻区分.Items.IndexOf(lst);
                }
                else
                {
                    ListItem lst = ddl_専攻区分.Items.FindByText("理系");
                    ddl_専攻区分.SelectedIndex = ddl_専攻区分.Items.IndexOf(lst);
                }

                //全日夜間
                if (dt.Rows[intRows]["全日夜間"].ToString() == "True")
                {
                    ListItem lst = ddl_全日夜間.Items.FindByText("夜間");
                    ddl_全日夜間.SelectedIndex = ddl_全日夜間.Items.IndexOf(lst);
                }
                else
                {
                    ListItem lst = ddl_全日夜間.Items.FindByText("全日");
                    ddl_全日夜間.SelectedIndex = ddl_全日夜間.Items.IndexOf(lst);
                }

                //専攻系列コード
                txt_専攻系列コード.Text = ConvertTexttoHTML(dt.Rows[intRows]["専攻系列コード"].ToString());

                //専攻学部
                txt_専攻学部.Text = ConvertTexttoHTML(dt.Rows[intRows]["専攻学部"].ToString());

                //専攻学科
                txt_専攻学科.Text = ConvertTexttoHTML(dt.Rows[intRows]["専攻学科"].ToString());

                //専攻内容
                txt_専攻内容.Text = ConvertTexttoHTML(dt.Rows[intRows]["専攻内容"].ToString());

                //専門技術
                txt_専門技術.Text = ConvertTexttoHTML(dt.Rows[intRows]["専門技術"].ToString());

                //Lay Id de update
                Session["DoubleClick_ID"] = dt.Rows[intRows]["ID"];

                //Xet truong hop add hoac update
                Session["Mode"] = "Update";
                btn_登録.Text = "更新";
            }
            
        }

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get object 応募学歴情報
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public 応募学歴情報 Get応募学歴情報(int temp)
    {
        応募学歴情報 _応募学歴情報 = new 応募学歴情報();
        //学歴区分     
        _応募学歴情報.学歴区分 = Convert.ToInt32(ddl_学歴区分.SelectedValue);

        //開始時期
        DateTime dt_開始時期 = new DateTime(Convert.ToInt32(ddl_入学年月日年.SelectedValue), Convert.ToInt32(ddl_入学年月日月.SelectedValue), 1);
        _応募学歴情報.開始時期 = dt_開始時期;

        //終了時期
        DateTime dt_終了時期 = new DateTime(Convert.ToInt32(ddl_卒業年月日年.SelectedValue), Convert.ToInt32(ddl_卒業年月日月.SelectedValue), 1);
        _応募学歴情報.終了時期 = dt_終了時期;

        //学校名
        _応募学歴情報.学校名 = ConvertHTMLtoText(txt_学校名.Text);

        //学校住所
        _応募学歴情報.学校住所 = ConvertHTMLtoText(txt_学校住所.Text);

        //専攻区分
        if (ddl_専攻区分.SelectedValue == "-1")
        {
            _応募学歴情報.専攻区分 = null;
        }
        else if (ddl_専攻区分.SelectedValue == "0")
        {
            _応募学歴情報.専攻区分 = 0;
        }
        else
        {
            _応募学歴情報.専攻区分 = 1;
        }
        

        //全日夜間
        if (ddl_全日夜間.SelectedValue == "-1")
        {
            _応募学歴情報.全日夜間 = null;
        }
        else if (ddl_全日夜間.SelectedValue == "0")
        {
            _応募学歴情報.全日夜間 = 0;
        }
        else
        {
            _応募学歴情報.全日夜間 = 1;
        }

        //専攻系列コード
        _応募学歴情報.専攻系列コード = ConvertHTMLtoText(txt_専攻系列コード.Text);
        

        //専攻学部
        _応募学歴情報.専攻学部 = ConvertHTMLtoText(txt_専攻学部.Text);

        //専攻学科
        _応募学歴情報.専攻学科 = ConvertHTMLtoText(txt_専攻学科.Text);

        //専攻内容
        _応募学歴情報.専攻内容 = ConvertHTMLtoText(txt_専攻内容.Text);

        //専門技術
        _応募学歴情報.専門技術 = ConvertHTMLtoText(txt_専門技術.Text);

        //応募情報ID nho sua lai so 60
        _応募学歴情報.応募情報ID = Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]);

        //廃止区分
        _応募学歴情報.廃止区分 = 0;

        //Option Update
        if(temp ==1)
        { 
            _応募学歴情報.ID = Convert.ToInt32( Session["DoubleClick_ID"]);
        }

        return _応募学歴情報;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character html to text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character text to html
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check insert data to table 応募学歴情報
    /// </summary>
    /// <param name="_応募学歴情報"></param>
    /// <returns></returns>
    public int frmJ_TraineeAcademicInput_Insert(応募学歴情報 _応募学歴情報)
    {
        return _応募学歴情報Manager.frmJ_TraineeAcademicInput_Insert(_応募学歴情報);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check update data to table 応募学歴情報
    /// </summary>
    /// <param name="_応募学歴情報"></param>
    /// <returns></returns>
    public int sp_frmJ_TraineeAcademicInput_Update_Dong(応募学歴情報 _応募学歴情報)
    {
        return _応募学歴情報Manager.frmJ_TraineeAcademicInput_Update(_応募学歴情報);
    }

    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Inset data to table 応募学歴情報
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        
        if (Convert.ToString(Session["Mode"]).Equals(""))
        {
            if (Session["frmJ_TraineeAcademicInput_Refresh"].ToString() == ViewState["frmJ_TraineeAcademicInput_Refresh"].ToString())
            {
                応募学歴情報 _応募学歴情報 = Get応募学歴情報(0);
                if (frmJ_TraineeAcademicInput_Insert(_応募学歴情報) == 1)
                {
                    //Xoa Tex
                    ClearText();
                    //ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                    ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                    Session["frmJ_TraineeAcademicInput_Refresh"] = Server.UrlEncode(System.DateTime.Now.ToString());
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
                }
            }
            else
            {
                ClearText();
                //ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
            }
           
        }
        else
        { 
            //update
            応募学歴情報 _応募学歴情報 = Get応募学歴情報(1);
            if (sp_frmJ_TraineeAcademicInput_Update_Dong(_応募学歴情報) == 1)
            {
                ClearText();
                //ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
                btn_登録.Text = "登録";
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
            }
        }
       
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Paging
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ViewState["page"] = e.NewPageIndex;
        grv.PageIndex = e.NewPageIndex;
        ShowGrv(Convert.ToInt32(Session["ID_frmJ_TraineeResumeAInput"]));
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Go page frmJ_TraineeAcademicInput
    /// </summary>
    public void ClearText()
    {
        Session.Remove("DoubleClick_ID");
        Session.Remove("Mode");
        Response.Redirect("frmJ_TraineeAcademicInput.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        ClearText();
        Session.Remove("DoubleClick_ID");
        Session.Remove("Mode");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get error message from id
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            error = dt.Rows[0][2].ToString();
        }
        return error;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert number to string
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string Show学歴区分(string temp)
    { 
        string strResult="";
        switch (temp)
        { 
            case "0":
                strResult = "博士";
                break;
            case "1":
                strResult = "修士";
                break;
            case "2":
                strResult = "学士";
                break;
            case "3":
                strResult = "高専";
                break;
            case "4":
                strResult = "高校";
                break;
            default:
                strResult = "中学";
                break;
        }
        return strResult;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get 1 record table 応募情報
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public DataTable frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(int ID)
    {
        return _応募情報Manager.frmJ_TraineeWorkedCompInput_Select生年月日_応募情報(ID);
    }
}
